package store

import (
	"gopkg.in/mgo.v2/bson"
	"main/database"
)

type Data struct {
	Time   int64     `json:"time" bson:"time"`
	IEDNO  int       `json:"iedno" bson:"iedno"`
	Sensor []float32 `json:"sensor" bson:"sensor"`
}

type ISensorService interface {
	GetData(i int) ([]Data, error)
}

type SensorService struct{}

var _ ISensorService = (*SensorService)(nil)

func NewSensorService() ISensorService {
	return &SensorService{}
}
func (s *SensorService) GetData(i int) ([]Data, error) {
	ds := database.NewSessionStore()
	defer ds.Close()
	con := ds.C("sensors")
	var res []Data
	err := con.Find(bson.M{"iedno": i}).All(&res)

	if err != nil {
		return []Data{}, err
	}
	return res[len(res)-10:], nil
}
